#include <butil/logging.h>
#include <brpc/server.h>
#include <iostream>
#include "cal.pb.h"

class CalcServiceImpl : public cal::CalService
{
public:
    CalcServiceImpl() {}
    ~CalcServiceImpl() {}
    void Add(::google::protobuf::RpcController* controller,
            const ::cal::AddReq* request,
            ::cal::AddRsp* response,
            ::google::protobuf::Closure* done) 
            {
                brpc::ClosureGuard done_guard(done);
                int result = request->num1() + request->num2();
                response->set_result(result);
            }
    void Hello(::google::protobuf::RpcController* controller,
            const ::cal::helloReq* request,
            ::cal::helloRsp* response,
            ::google::protobuf::Closure* done) override 
            {
                brpc::ClosureGuard done_guard(done);
                auto cntl = static_cast<brpc::Controller*>(controller);
                const auto& headers = cntl->http_request();
                std::cout << "Method: " << brpc::HttpMethod2Str(headers.method()) << std::endl;
                std::cout << "Path: " << cntl->request_attachment().to_string() << std::endl;
            }
};

int main(int argc, char* argv[])
{
    CalcServiceImpl cal_service;

    brpc::ServerOptions options;
    options.idle_timeout_sec = -1;

    brpc::Server server;

    int ret = server.AddService(&cal_service, brpc::SERVER_DOESNT_OWN_SERVICE);
    if(ret == -1)
    {
        std::cout << "AddService failed" << std::endl;
        return -1;
    }

    ret = server.Start(9000, &options);
    if(ret == -1)
    {
        std::cout << "Start failed" << std::endl;
        return -1;
    }
    
    server.RunUntilAskedToQuit();

    return 0;
}


